home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / KEYBOARD.SWG / 0054_Nice Keyboard Handler.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  3KB  |  117 lines

  1. UNIT KeyIntr;  { support for INT 09 16 routines } { Turbo Pascal 5.5+ }
  2.  
  3. INTERFACE
  4.  
  5. Type
  6.   InterruptProcedure = Procedure;
  7.  
  8. Const
  9.   BiosDataSegment = $40;
  10.  
  11. Procedure DisableInterrupts; Inline($FA);   { CLI }
  12. Procedure EnableInterrupts;  Inline($FB);   { STI }
  13. Procedure CallInterrupt(P : Pointer);
  14.  
  15. Function AltPressed : Boolean;
  16. Function ControlPressed : Boolean;
  17. Function ShiftPressed : Boolean;
  18.  
  19. Procedure EOI;                      { end of interrupt to 8259 }
  20. Function  ReadScanCode : Byte;       { read keyboard }
  21. Procedure ResetKeyboard;            { prepare for next key }
  22.                                      { put key in buffer for INT 16 }
  23. Function StoreKey(Scan, Key : Byte) : Boolean;
  24.  
  25. IMPLEMENTATION
  26.  
  27. Type
  28.   TwoBytesPtr = ^TwoBytes;
  29.   TwoBytes = Record  { one key in the keyboard buffer }
  30.     KeyCode,
  31.     ScanCode : Byte;
  32.   End;
  33.  
  34. Var
  35.   KeyState       : Word Absolute BiosDataSegment : $17;
  36.   KeyBufferHead  : Word Absolute BiosDataSegment : $1A;
  37.   KeyBufferTail  : Word Absolute BiosDataSegment : $1C;
  38.   KeyBufferStart : Word Absolute BiosDataSegment : $80;
  39.   KeyBufferEnd   : Word Absolute BiosDataSegment : $82;
  40.  
  41. Procedure CallInterrupt(P : Pointer);
  42. Begin
  43.   Inline($9C);           { PUSHF }
  44.   InterruptProcedure(P);
  45. End;
  46.  
  47. Function AltPressed : Boolean;
  48. Begin
  49.   AltPressed := (KeyState and 8) <> 0;
  50. End;
  51.  
  52. Function ControlPressed : Boolean;
  53. Begin
  54.   ControlPressed := (KeyState and 4) <> 0;
  55. End;
  56.  
  57. Function ShiftPressed : Boolean;
  58. Begin
  59.   ShiftPressed := (KeyState and 3) <> 0;
  60. End;
  61.  
  62. Procedure EOI;  { end of interrupt to 8259 interrupt controller }
  63. Begin
  64.   Port[$20] := $20;
  65. End;
  66.  
  67. Function ReadScanCode : Byte;
  68. Var
  69.   N : Byte;
  70. Begin
  71.   N := Port[$60];     { $FF means keyboard overrun }
  72.   ReadScanCode := N;
  73. End;
  74.  
  75. Procedure ResetKeyboard;      { prepare for next key }
  76. Var
  77.   N : Byte;
  78. Begin
  79.   N := Port[$61];
  80.   Port[$61] := (N or $80);
  81.   Port[$61] := N;
  82. End;
  83.  
  84. Function StoreKey(Scan, Key : Byte) : Boolean;
  85. Var                { put key in buffer that INT 16 reads }
  86.   P : TwoBytesPtr;
  87.   N : Word;
  88. Begin
  89.   DisableInterrupts;
  90.  
  91.   N := KeyBufferTail;
  92.   P := Ptr(BiosDataSegment, N);
  93.  
  94.   Inc(N, 2);
  95.   If(N = KeyBufferEnd) then        { end of the circular buffer }
  96.     N := KeyBufferStart;
  97.   If(N = KeyBufferHead) then       { buffer full }
  98.   Begin
  99.     EnableInterrupts;
  100.     StoreKey := False;
  101.   End
  102.   Else
  103.   Begin
  104.     P^.KeyCode := Key;
  105.     P^.ScanCode := Scan;             { store key in circular buffer }
  106.     KeyBufferTail := N;              { advance tail pointer }
  107.     EnableInterrupts;
  108.     StoreKey := True;
  109.   End;
  110. End;
  111.  
  112.  
  113. END.
  114.  
  115.  
  116.  
  117.